package com.gopay.scheduler.job;
import com.gopay.common.constants.mail.MailLevel;
import com.gopay.common.constants.mail.MailTemplateType;
import com.gopay.common.domain.mail.Send;
import com.gopay.common.util.StringTools;
import com.gopay.remote.cps.mail.MailRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.dao.qulifications.OverQualificationsInfoReadDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * CLASSNAME:AutoGenIsOverQualificationsNotifyJob
 * Description:到期过期邮件通知和锁定同一custid下的所有acctid
 * DATE:2018/6/11/011
 * AUTHER:PANSHICHAO
 * www.gopay.com.cn Inc.All rights reserved.
 */
public class AutoGenIsOverQualificationsNotifyJob extends GopayBaseJob {
    @Autowired
    private OverQualificationsInfoReadDao overQualificationsInfoReadDao;
    @Resource(name="mailRemoteService")
    private MailRemoteService mailRemoteService;
    private @Value("#{webProperties['portal.url']}") String portalUrl;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    protected void doJobInternal() {
        Date today = new Date();
        Calendar c=Calendar.getInstance();
        c.setTime(today);
        int weekday=c.get(Calendar.DAY_OF_WEEK);
        long currntMillions = today.getTime();
        long endMillions = currntMillions + 15 *24*3600*1000;
        Date endDate = new Date(endMillions);
        List<Object[]> result = overQualificationsInfoReadDao.getAllQualificationsInfo(today,endDate);
        JSONArray json1 = JSONArray.fromObject(result);
        String str = json1.toString();//把json转换为String
        logger.info("即将资质过期的对象："+str);
        if(weekday == 2){
            logger.info("今天是星期一，需要执行资质将要过期提醒任务！");
            if(result !=null && !result.isEmpty()){
                for(Object [] objs : result){
                    String receiversEmail = StringTools.checkNulltoString(objs[2]);
                    if(StringUtils.isNotBlank(receiversEmail)){
                        Send send = new Send();
                        send.setReceivers(receiversEmail);
                        send.setTemplate(MailTemplateType.EMAIL_WILL_QUALIFICATIONS_SEND.value);
                        send.setLevel(MailLevel.MID);
                        Map<String,Object> json = new HashMap<String,Object>(3);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
                        String overBussinessTerm = "";
                        String overLegalPersonCertTerm="";
                        if(objs[3] != null){
                            Date d = (Date) objs[3];
                            if(d.getTime() - today.getTime() <15 *24*3600*1000){
                                overLegalPersonCertTerm = format.format(d);
                            }
                        }
                        if(objs[4] != null){
                            Date d = (Date)objs[4];
                            if(d.getTime() - today.getTime() <15 *24*3600*1000){
                                overBussinessTerm=format.format(d);
                            }
                        }
                        if(org.apache.commons.lang3.StringUtils.isNotBlank(overBussinessTerm)){
                            String overBussinessTermVal = "<li>营业执照将于"+overBussinessTerm+"到期</li>";
                            json.put("businessTermVal",overBussinessTermVal);
                        }else{
                            json.put("businessTermVal","");
                        }
                        if(org.apache.commons.lang3.StringUtils.isNotBlank(overLegalPersonCertTerm)){
                            String overLegalPersonCertTermVal = "<li>法定代表人证件将于"+overLegalPersonCertTerm +"到期</li>";
                            json.put("legalPersonCertTermVal",overLegalPersonCertTermVal);
                        }else{
                            json.put("legalPersonCertTermVal","");
                        }
                        json.put("replacethistotheLogoAddr", portalUrl);
                        send.setContent(JSONObject.fromObject(json).toString());
                        int i =mailRemoteService.addMailTask(send);
                        logger.info("邮件发送ID为："+i);
                    }
                }
            }
        }else{
            logger.info("今天不是周一无需执行该任务");
        }
    }
}
